package com.abel;

import io.netty.util.concurrent.*;

import java.util.LinkedList;
import java.util.concurrent.*;
import java.util.concurrent.Future;

/**
 * @Author lhong
 * @Date 2017/5/5 16:09
 */
public class TaskCallResult implements Callable {

    private int i;
    public TaskCallResult(int i){
        this.i=i;
    }

    public String call() throws Exception {
        System.out.println("执行线程"+i);
        return "返回结果"+Thread.currentThread().getName();
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        LinkedList<Future> futures = new LinkedList<Future>();
        for (int i = 0; i < 10; i++) {
            Future submit = executorService.submit(new TaskCallResult(i));
            futures.add(submit);
        }
        for (Future f:futures) {
            while(!f.isDone());
            try {
                System.out.println(f.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        executorService.shutdown();
    }
}
